import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Role } from './entities/role.entity';
import { MenuService } from '../menu/menu.service';
import { Menu } from '../menu/entities/menu.entity';

@Injectable()
export class RoleService {
  constructor(
    @InjectRepository(Role)
    private readonly roleRep: Repository<Role>,
    private readonly menuService: MenuService
  ) { }

  async findAll() {
    const roleData = await this.roleRep.find({
      relations: ["menu"],
    });
    // console.log(roleData);
    return roleData;
  }

  async create(role: any) {
    // console.log(role);

    // const metaList = await this.menuService.metaFindByIds(role.menuIds)
    // role.menu = metaList
    // const newRole = await this.roleRep.create(role);
    // console.log(newRole);

    role.menu = role.menu.map((item) => Object.assign(new Menu(), { id: item }))
    // console.log(metaList);
    // console.log(newRole);
    const saveRole = await this.roleRep.save(role)
    return saveRole

  }

  async remove(id: string) {
    return await this.roleRep.delete(id)
  }

}
